package com.rowline.admin.module.study.jdknewfeature;

import java.util.Optional;

/**
 * ClassName:OptionalDemo
 * Created by 245167
 * on 2018/5/5  05:54
 * description:
 */
public class OptionalDemo {

    public static void main(String[] args) {

        // Optional.ofNullable()  ==>  参数可以是null
        Optional< String > fullName = Optional.ofNullable( null );
        System.out.println( "Full Name is set? " + fullName.isPresent() );
        System.out.println( "Full Name: " + fullName.orElseGet( () -> "[none]" ) );
        System.out.println( fullName.map( s -> "Hey " + s + "!" ).orElse( "Hey Stranger!" ) );

        System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");

        // Optional.of()  ==>  参数不能是null
        Optional< String > firstName = Optional.of( "Tom" );
        System.out.println( "First Name is set? " + firstName.isPresent() );
        System.out.println( "First Name: " + firstName.orElseGet( () -> "[none]" ) );
        System.out.println( firstName.map( s -> "Hey " + s + "!" ).orElse( "Hey Stranger!" ) );
        System.out.println();

        // java.lang.NullPointerException
//        Optional< String > lastName = Optional.of(null);
//        System.out.println( "Last Name is set? " + lastName.isPresent() );



    }


}
